# -*- coding: utf-8 -*-
# @version : 1.0
# @File    : __init__.py.py
# @Date    : 2024/12/26 10:51
# @Author  : GlenZhao
# @Desc    :
from sqlalchemy import Integer, String, Boolean, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column

from db.base import BaseModel


class AuthMenu(BaseModel):
    __tablename__ = "auth_menu"
    __table_args__ = ({'comment': '菜单表'})

    title: Mapped[str] = mapped_column(String(50), comment="名称")
    icon: Mapped[str | None] = mapped_column(String(50), comment="菜单图标")
    redirect: Mapped[str | None] = mapped_column(String(100), comment="重定向地址")
    component: Mapped[str | None] = mapped_column(String(255), comment="前端组件地址")
    path: Mapped[str | None] = mapped_column(String(50), comment="前端路由地址")
    disabled: Mapped[bool] = mapped_column(Boolean, default=False, comment="是否禁用")
    hidden: Mapped[bool] = mapped_column(Boolean, default=False, comment="是否隐藏")
    order: Mapped[int] = mapped_column(Integer, comment="排序")
    menu_type: Mapped[str] = mapped_column(String(8), comment="菜单类型")
    parent_id: Mapped[int | None] = mapped_column(
        Integer,
        ForeignKey("auth_menu.id", ondelete='CASCADE'),
        comment="父菜单"
    )
    perms: Mapped[str | None] = mapped_column(String(50), comment="权限标识", unique=False, index=True)

    @property
    def name(self):
        if self.title.startswith("menus."):
            return self.title.replace("menus.", "")
        return self.title
